//
//  SettingView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/16.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SettingView.h"
#import "SettingInputView.h"
#import "ZZRadioControl.h"

#define LeftWidth SCREEN_WIDTH / 3

@interface SettingView()<ZZRadioControlDelegate, SettingInputViewDelegate>

@property (nonatomic, strong) UIView *left_info_view;
@property (nonatomic, strong) UIImageView *avatar_image_view;
@property (nonatomic, strong) UIButton *login_out_button;

@property (nonatomic, strong) SettingInputView *nameInputView;
@property (nonatomic, strong) SettingInputView *areaInputView;
@property (nonatomic, strong) SettingInputView *signInputView;

@property (nonatomic, strong) UIButton *boy_button;
@property (nonatomic, strong) UIButton *girl_button;

@end

@implementation SettingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeSettingView];
        
    }
    return self;
}

- (void)makeSettingView
{
    _left_info_view = [UIView new];
    [self addSubview:_left_info_view];
    [_left_info_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(LeftWidth);
    }];
    
    _avatar_image_view = [UIImageView new];
    _avatar_image_view.backgroundColor = [UIColor yellowColor];
    _avatar_image_view.userInteractionEnabled = YES;
    _avatar_image_view.layer.masksToBounds = YES;
    _avatar_image_view.layer.borderWidth = 0;
    _avatar_image_view.layer.cornerRadius = LeftWidth / 4;
    [_left_info_view addSubview:_avatar_image_view];
    [_avatar_image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(LeftWidth / 2, LeftWidth / 2));
        make.centerX.mas_equalTo(_left_info_view);
        make.centerY.mas_equalTo(-50);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(touchAvatarImage:)];
    [_avatar_image_view addGestureRecognizer:tap];
    
    _login_out_button = [UIButton new];
    [_login_out_button setBackgroundColor:RGB(214, 65, 43)];
    [_login_out_button setTitle:@"退出登陆" forState:UIControlStateNormal];
    [_login_out_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _login_out_button.layer.cornerRadius = 5.0;
    [_login_out_button addTarget:self action:@selector(clickLoginOutButton:) forControlEvents:UIControlEventTouchUpInside];
    [_left_info_view addSubview:_login_out_button];
    [_login_out_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_avatar_image_view.mas_bottom).mas_offset(40);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(36);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor blackColor];
    [_left_info_view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.bottom.mas_equalTo(-40);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(0.5);
    }];
    
    
    UIView *rightView = [UIView new];
    [self addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_left_info_view.mas_right).mas_offset(0);
        make.top.bottom.right.mas_equalTo(0);
    }];
    
    _nameInputView = [SettingInputView new];
    _nameInputView.labelTitle = @"昵称";
    _nameInputView.placeholder = @"修改昵称";
    _nameInputView.tag = 201;
    _nameInputView.delegate = self;
    [rightView addSubview:_nameInputView];
    [_nameInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.height.mas_equalTo(44);
    }];
    
    _areaInputView = [SettingInputView new];
    _areaInputView.labelTitle = @"地区";
    _areaInputView.placeholder = @"修改地区";
    _areaInputView.tag = 202;
    _areaInputView.delegate = self;
    [rightView addSubview:_areaInputView];
    [_areaInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.height.mas_equalTo(44);
    }];
    
    _signInputView = [SettingInputView new];
    _signInputView.labelTitle = @"目标";
    _signInputView.placeholder = @"修改目标";
    _signInputView.tag = 203;
    _signInputView.delegate = self;
    [rightView addSubview:_signInputView];
    [_signInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_areaInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.height.mas_equalTo(44);
    }];
    
    
    UILabel *sexLabel = [UILabel new];
    sexLabel.text = @"性别";
    sexLabel.font = [UIFont systemFontOfSize:18.0f];
    sexLabel.textColor = [UIColor blackColor];
    [rightView addSubview:sexLabel];
    [sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_signInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(40);
        make.size.mas_equalTo(CGSizeMake(65, 20));
    }];
    
    UIImageView *sex_boy_image = [UIImageView new];
    sex_boy_image.image = [UIImage imageNamed:@"regist_sex_boy_image"];
    [rightView addSubview:sex_boy_image];
    [sex_boy_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_signInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(sexLabel.mas_right).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    

    _boy_button = [UIButton new];
    [_boy_button setImage:[UIImage imageNamed:@"check_box_unselect"] forState:UIControlStateNormal];
    [_boy_button setImage:[UIImage imageNamed:@"check_box_select"] forState:UIControlStateSelected];
    [_boy_button addTarget:self action:@selector(selectBoySexButton:) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:_boy_button];
    [_boy_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_signInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(sex_boy_image.mas_right).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    UIImageView *sex_girl_image = [UIImageView new];
    sex_girl_image.image = [UIImage imageNamed:@"regist_sex_girl_image"];
    [rightView addSubview:sex_girl_image];
    [sex_girl_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_signInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(_boy_button.mas_right).mas_offset(30);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    _girl_button = [UIButton new];
    [_girl_button setImage:[UIImage imageNamed:@"check_box_unselect"] forState:UIControlStateNormal];
    [_girl_button setImage:[UIImage imageNamed:@"check_box_select"] forState:UIControlStateSelected];
    [_girl_button addTarget:self action:@selector(selectGirlSexButton:) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:_girl_button];
    [_girl_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_signInputView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(sex_girl_image.mas_right).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];

    
}

- (void)selectBoySexButton:(UIButton *)button
{
    _girl_button.selected = NO;
    _boy_button.selected = YES;
    if ([self.delegate respondsToSelector:@selector(clickUserBoySexUpload:)]) {
        [self.delegate clickUserBoySexUpload:1];
    }
}

- (void)selectGirlSexButton:(UIButton *)button
{
    _boy_button.selected = NO;
    _girl_button.selected = YES;
    if ([self.delegate respondsToSelector:@selector(clickUserGirlSexUpload:)]) {
        [self.delegate clickUserGirlSexUpload:2];
    }
}

- (void)refreshUserData:(UserModel *)user
{
    [_avatar_image_view sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,user.avatar]]];
    _nameInputView.field.text = user.username;
    
    _areaInputView.field.text = [NSString metaEmptyString:user.address];
    
    _signInputView.field.text = user.sign;
    
    if ([user.sex integerValue] == 1) {
        _boy_button.selected = YES;
        _girl_button.selected = NO;
    }else if ([user.sex integerValue] == 2){
        _boy_button.selected = NO;
        _girl_button.selected = YES;
    }
}

- (void)touchAvatarImage:(UITapGestureRecognizer *)touch
{
    if ([self.delegate respondsToSelector:@selector(clickAavtarUpload)]) {
        [self.delegate clickAavtarUpload];
    }
}

- (void)clickLoginOutButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(clickLoginOut)]) {
        [self.delegate clickLoginOut];
    }
}

- (void)clickSubmit:(SettingInputView *)inputView inputText:(NSString*)text
{
    if (inputView.tag == 201) {
        if ([self.delegate respondsToSelector:@selector(clickUsernameUpload:)]) {
            [self.delegate clickUsernameUpload:text];
        }
    }else if (inputView.tag == 202){
        if ([self.delegate respondsToSelector:@selector(clickUserAreaUpload:)]) {
            [self.delegate clickUserAreaUpload:text];
        }
    }else if (inputView.tag == 203){
        if ([self.delegate respondsToSelector:@selector(clickUserSignUpload:)]) {
            [self.delegate clickUserSignUpload:text];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_nameInputView.field resignFirstResponder];
    [_areaInputView.field resignFirstResponder];
    [_signInputView.field resignFirstResponder];
}

@end
